package com.enzo.pcms.login.service;

import com.enzo.pcms.common.util.ConsUtil;
import com.enzo.pcms.common.util.MsgUtil;
import com.enzo.pcms.login.dao.UserDao;
import com.enzo.pcms.login.model.User;
import com.itool.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;

@Component("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    public Integer insert(User user)throws Exception {
        return userDao.insert(user);
    }

    public void checkUserByNameOrEmail(String userName,String email)throws Exception{
        userDao.checkUserByNameOrEmail(userName,email);
    }


    public String activateUser(String activateCode)throws Exception{
        if(StringUtil.isNotEmpty(activateCode)){
            User user=userDao.getUserByActivateCode(activateCode);
            if(user!=null){
                if(user.getActivateFlag()==1){
                    //user has activated
                    return MsgUtil.getValue(ConsUtil.MSG_ACTIVATE_ACTIVATED);
                }else{
                    if(isOverdue(user.getUpdateTmp())){
                        //user is overdue TODO
                        return MsgUtil.getValue(ConsUtil.MSG_ACTIVATE_ISOVERDUE);
                    }else{
                        //activate success TODO
                        user.setActivateFlag(1);
                        user.setUpdateTmp(DateUtil.getCurrentDateTime());
                        userDao.activateUser(user);
                        return MsgUtil.getValue(ConsUtil.MSG_ACTIVATE_SUCCESS);
                    }
                }
            }else{
                //user not exist TODO
                return MsgUtil.getValue(ConsUtil.MSG_ACTIVATE_NOTEXIST);
            }
        }
        return null;
    }


    public String signUp(User user,String vCodeInput,String vCodeSession)throws Exception{
        String msg=null;
        //判断验证码
        if(vCodeSession.equals(vCodeInput)){
            //设置注册激活码
            user.setActivateCode(UUIDUtil.getUUID());
            userDao.insert(user);
            msg=MsgUtil.getValue(ConsUtil.MSG_REGISTER_SUCCESS,new String[]{user.getEmail()});
            String link="http://localhost:8080/pcms/user/activate?key="+user.getActivateCode();
            //TODO 异步发送激活邮件 使用线程或消息组件
            MailUtil.sendActivateMail(user.getEmail(), link);
        }else{
            msg=MsgUtil.getValue(ConsUtil.MSG_REGISTER_VALIDCODE_ERROR);
        }
        return msg;
    }

    /**
     * check if is overdue
     * @return
     */
    private boolean isOverdue(Timestamp t){
        long now=DateUtil.getCurrentDateTime().getTime();
        long registerTmp=t.getTime();
        long overdue=Long.parseLong(PropertiesUtil.getValue(ConsUtil.ACTIVATE_OVERDUE))*60*60*1000;
        return now-registerTmp>overdue?true:false;
    }

    public User getUserByEmail(String email) throws Exception {
        return userDao.getUserByNameOrEmail(null, email);
    }

    public UserDao getUserDao() {
        return userDao;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }



}
